package com.controller;

import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.swing.plaf.basic.BasicHTML;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Scanner;

@Controller
public class AuthController extends BaseController {
    @Autowired
    Producer producer;
    @GetMapping("/capthca.jpg")
    public void kaptcha(HttpServletResponse response) throws IOException {
        String text = producer.createText();
//        Scanner scanner = new Scanner(System.in);//自定义字符的验证码
//         text = scanner.nextLine();
        BufferedImage image = producer.createImage(text);
        //因为只是一个验证码（每次刷新就不存在了），所以不需要缓存
        response.setHeader("Cache-Control", "no-store,no-cache");
        //设置响应头为图片类型
        response.setContentType("image/jpeg");

        //返回图片给客户端，将图片放在图片流中
        ServletOutputStream outputStream = response.getOutputStream();
        ImageIO.write(image, "jpg",outputStream);
    }
}
